//
//  MineOrderCell.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/14.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "MineOrderCell.h"
#import "OrderCartView.h"
#import "OrderEntity.h"
#import "ShoppingCartEntity.h"

@interface MineOrderCell ()

@property (nonatomic, strong) OrderCartView *cartView;
@property (nonatomic, strong) UIButton *changeStateBtn;

@end

@implementation MineOrderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setOrderEntity:(OrderEntity *)orderEntity
{
    _orderEntity = orderEntity;
    if (orderEntity.state.intValue == 0) {
        self.cartView.orderState.text = @"待付款";
        self.changeStateBtn.hidden = YES;
    }
    else if (orderEntity.state.intValue == 1) {
        self.cartView.orderState.text = @"待发货";
        self.changeStateBtn.hidden = YES;
    }
    else if (orderEntity.state.intValue == 2) {
        self.cartView.orderState.text = @"待收货";
        self.changeStateBtn.hidden = NO;
        [self.changeStateBtn setTitle:@"确认收货" forState:UIControlStateNormal];
    }
    else if (orderEntity.state.intValue == 3) {
        self.cartView.orderState.text = @"已完成";
        self.changeStateBtn.hidden = NO;
        [self.changeStateBtn setTitle:@"删除" forState:UIControlStateNormal];
    }
    else {
        self.cartView.orderState.text = @"关闭";
        self.changeStateBtn.hidden = NO;
        [self.changeStateBtn setTitle:@"删除" forState:UIControlStateNormal];
    }
    self.cartView.shopNameLab.text = orderEntity.consumerName;
    self.cartView.tradeNameLab.text = orderEntity.goodsName;
    self.cartView.tradeDesLab.text = orderEntity.summary;
    self.cartView.tradeTimeLab.text = String(orderEntity.goodsParam);
    self.cartView.tradeNumLab.text = [NSString stringWithFormat:@"x%@",orderEntity.dealNum];
    self.cartView.price = orderEntity.price;
    self.cartView.memberPrice = orderEntity.memberPrice;
    float price = orderEntity.deaAmount.floatValue;
    self.cartView.payPriceLab.text = [NSString stringWithFormat:@"￥%.2f",price];
    self.cartView.payLab.text = [NSString stringWithFormat:@"共%@件，合计：",orderEntity.dealNum];
    [self.cartView.tradeIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVERADDRS,orderEntity.filePath]] placeholderImage:PlaceHolderImage];
}

- (void)setEntity:(ShoppingCartEntity *)entity
{
    _entity = entity;
    self.cartView.shopNameLab.text = entity.consumerName;
    self.cartView.tradeNameLab.text = entity.goodsName;
    self.cartView.tradeDesLab.text = entity.summary;
    self.cartView.tradeNumLab.text = entity.purchaseNum;
    [self.cartView.goodsNumSelectView setNumber:entity.purchaseNum];
    self.cartView.tradeTimeLab.text = entity.goodsParams;
    
    self.cartView.isSelect = entity.isSelect.boolValue;
    
    self.cartView.price = entity.price;
    self.cartView.memberPrice = entity.memberPrice;
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",SERVERADDRS,entity.filePath];
    [self.cartView.tradeIcon sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:PlaceHolderImage];
}

- (void)setupSubViews
{
    self.backgroundColor = kColorF2F2F2;
    self.cartView = [[OrderCartView alloc] init];
    [self addSubview:self.cartView];
    self.cartView.backgroundColor = [UIColor whiteColor];
    self.cartView.layer.cornerRadius = 4;
    self.cartView.layer.masksToBounds = YES;
    [self.cartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(kWidth(10));
        make.left.equalTo(self).offset(kWidth(15));
        make.right.equalTo(self).offset(-kWidth(15));
        make.bottom.equalTo(self);
    }];
    
    self.changeStateBtn = [[UIButton alloc] init];
    self.changeStateBtn.backgroundColor = kColorFF2C2C;
    self.changeStateBtn.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    self.changeStateBtn.contentEdgeInsets = UIEdgeInsetsMake(2, 4, 2, 4);
    [self.changeStateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.changeStateBtn.layer.cornerRadius = 2;
    self.changeStateBtn.layer.masksToBounds = YES;
    self.changeStateBtn.hidden = YES;
    [self.cartView addSubview:self.changeStateBtn];
    [self.changeStateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cartView).offset(kWidth(15));
        make.bottom.equalTo(self.cartView).offset(-kWidth(10));
    }];
    
    WeakSelf
    self.cartView.selectHandler = ^(int num) {
        if (weakSelf.selectHandler) {
            weakSelf.selectHandler(num);
        }
    };
    
    self.cartView.addOrDeleteHandler = ^(int num) {
        if (weakSelf.addOrDeleteHandler) {
            weakSelf.addOrDeleteHandler(num);
        }
    };
    
    [self.changeStateBtn addTarget:self action:@selector(changeAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setCanSelect:(BOOL)canSelect
{
    _canSelect = canSelect;
    if (canSelect) {
        self.cartView.type = OrderCartViewSelect;
    }
    else {
        self.cartView.type = OrderCartViewNormal;
    }
}

- (void)changeAction
{
    if (self.changeStateHandler) {
        self.changeStateHandler();
    }
}

@end
